<template>
  <div>
    <div class="supplier" v-if="!dataAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-hasPermi="['system:dict:query']"
          class="btn search"
          @click="handleQuery"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:dict:add']"
          class="btn search"
          @click="handleAdd"
          ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
          >新增</el-button
        >
        <el-button class="btn direction" @click="handleBack"
          ><el-icon class="icon" color="#fff"> <Back /> </el-icon
          >返回</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-select
            v-model="queryParams.dictType"
            style="width: 200px"
            placeholder="请选择字典名称"
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.dictId"
              :label="item.dictName"
              :value="item.dictType"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-input
            v-model="queryParams.dictLabel"
            placeholder="请输入字典标签"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </div>
        <div class="search-item">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择数据状态"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        border
        height="64vh"
        v-if="refreshTable"
        v-loading="loading"
        :data="dataList"
        row-key="dataId"
        :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        :header-cell-style="{ 'text-align': 'center', background: 'red' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="150px"
          align="center"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-hasPermi="['system:dict:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-hasPermi="['system:dict:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-hasPermi="['system:dict:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column label="字典编码" align="center" prop="dictCode" />
        <el-table-column label="字典标签" align="center" prop="dictLabel">
          <template #default="scope">
            <span
              v-if="
                (scope.row.listClass == '' ||
                  scope.row.listClass == 'default') &&
                (scope.row.cssClass == '' || scope.row.cssClass == null)
              "
              >{{ scope.row.dictLabel }}</span
            >
            <el-tag
              v-else
              :type="
                scope.row.listClass == 'primary' ? '' : scope.row.listClass
              "
              :class="scope.row.cssClass"
              >{{ scope.row.dictLabel }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="字典键值" align="center" prop="dictValue" />
        <el-table-column label="字典排序" align="center" prop="dictSort" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="createBy"
          label="创建人"
          width="120"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']"
              >修改</el-button
            >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <DataAdd
      v-if="dataAddShow"
      ref="dataAddRef"
      v-model:dataAddShow="dataAddShow"
      @refreshList="handleQuery"
      :pageType="pageType"
    />
  </div>
</template>

<script setup name="Data">
import DataAdd from "./dataAdd";
import {
  optionselect as getDictOptionselect,
  getType,
} from "@/api/system/dict/type";
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from "@/api/system/dict/data";
import { ElMessage, ElMessageBox } from "element-plus";
const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const total = ref(0);
const dataOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);
const showChooseIcon = ref(false);
const dataAddShow = ref(false);
const iconSelectRef = ref(null);
const dataAddRef = ref(null);
const pageType = ref("");
const dateRange = ref([]);
const typeOptions = ref([]);
const defaultDictType = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    dataCode: undefined,
    dataName: undefined,
    status: undefined,
  },
  rules: {
    dataName: [
      { required: true, message: "菜单名称不能为空", trigger: "blur" },
    ],
    orderNum: [
      { required: true, message: "菜单顺序不能为空", trigger: "blur" },
    ],
    path: [{ required: true, message: "路由地址不能为空", trigger: "blur" }],
  },
});

const { queryParams, form } = toRefs(data);
/** 查询字典类型列表 */
function getList() {
  loading.value = true;
  listData(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    dataId: undefined,
    parentId: 0,
    dataName: undefined,
    icon: undefined,
    dataType: "M",
    orderNum: undefined,
    isFrame: "1",
    isCache: "0",
    visible: "0",
    status: "0",
  };
  proxy.resetForm("dataRef");
}

const handleOperate = async (type, row) => {
  switch (type) {
    case "detail":
      dataAddShow.value = true;
      pageType.value = "detail";
      nextTick(() => {
        dataAddRef.value.handleDetailInfo(row.dictCode);
      });
      break;
    case "edit":
      dataAddShow.value = true;
      pageType.value = "edit";
      nextTick(() => {
        dataAddRef.value.handleDetailInfo(row.dictCode);
      });
      break;
    case "delete":
      ElMessageBox.confirm("此操作将删除该字典数据，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await delData(row.dictCode);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            handleQuery();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

// 处理返回
const handleBack = () => {
  const obj = { path: "/system/dict" };
  proxy.$tab.closeOpenPage(obj);
};

// 处理新增
const handleAdd = () => {
  dataAddShow.value = true;
  pageType.value = "add";
  nextTick(() => {
    dataAddRef.value.handleParentId(queryParams.value.dictType);
  });
};

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 查询字典类型列表 */
function getTypeList() {
  getDictOptionselect().then((response) => {
    typeOptions.value = response.data;
  });
}

/** 查询字典类型详细 */
function getTypes(dictId) {
  getType(dictId).then((response) => {
    queryParams.value.dictType = response.data.dictType;
    defaultDictType.value = response.data.dictType;
    getList();
  });
}

// -----------------------------------  序号  ----------------------------------
function indexMethod(index) {
  // 当前页码
  const currentPage = queryParams.value.pageNum;
  // 每页显示的数据条数
  const pageSize = queryParams.value.pageSize;
  // 当前行的实际索引
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  // 如果当前行的索引大于等于当前页的最大索引，
  // 则使用当前行的实际索引，否则使用当前行的原始索引
  return truelndex;
}

// getList();
getTypes(route.params && route.params.dictId);
getTypeList();
</script>
<style>
.confirmBtnClass {
  color: #fff;
  width: 90px;
  height: 34px;
  background: #007cef;
}
.confirmBtnClass:hover,
.confirmBtnClass:active,
.confirmBtnClass:focus {
  color: #fff;
  background: #007cef;
}
.cancelBtnClass {
  border: 1px solid #999999;
  color: #333333;
  width: 90px;
  height: 34px;
  background: #f3f3f3;
}
.cancelBtnClass:hover,
.cancelBtnClass:active,
.cancelBtnClass:focus {
  border: 1px solid #999999;
  color: #333333;
  background: #f3f3f3;
}
</style>
<style lang="scss" scoped>
:deep(.table-header) {
  background-color: #f8faff !important;
}

.supplier {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  .search {
    display: flex;
    margin-bottom: 20px;
    .search-item {
      margin-right: 20px;
    }
    .search-input {
      width: 200px;
      height: 34px;
    }
  }
  /* .search-button {
    position: absolute;
    top: -45px;
    right: 60px;
    display: flex;
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;
      height: 34px;
      padding: 10px;
      // margin-right: 20px;
      font-size: 14px;
      border-radius: 4px;
      user-select: none;
      cursor: pointer;
    }
    .btn.search {
      color: #fff;
      background: rgba(0, 124, 239, 1);
      &:hover {
        background: rgba(0, 124, 239, 0.85);
      }
    }
    .btn.add {
      border: none;
      color: #fff;
      background: rgba(255, 159, 0, 1);
      &:hover {
        background: rgba(255, 159, 0, 0.85);
      }
    }

    .btn.direction {
      border: none;
      color: #fff;
      background: rgba(153, 153, 153, 1);
      &:hover {
        background: rgba(153, 153, 153, 0.85);
      }
    }
    .btn.reset {
      color: rgba(43, 193, 123, 1);
      background: rgba(43, 193, 123, 0.05);
      border: 1px solid rgba(43, 193, 123, 1);
      &:hover {
        background: rgba(43, 193, 123, 0.2);
      }
    }
    .icon {
      margin-right: 6px;
    }
  } */
  .table-operate {
    display: flex;
    justify-content: space-around;
    .table-icon {
      width: 20px;
      height: 20px;
      cursor: pointer;
    }
  }
  .pagination {
    margin-top: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
  :deep(.el-table) {
    th.el-table__cell {
      font-size: 14px;
      font-weight: 400;
      height: 40px;
      color: #333333;
      background: #f8faff !important;
    }
    // 表格高度
    .el-table__cell {
      padding: 0;
      height: 40px;
    }
  }
  :deep(.el-table--large .cell) {
    padding: 0 20px 0 20px !important;
  }
}
</style>
